package com.tuozixuan.graphql;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.tuozixuan.graphql.company.model.Company;
import com.tuozixuan.graphql.company.service.CompanyService;

import graphql.schema.DataFetcher;

@Component
public class CompanyGraphQLDataFetchers {
    
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Autowired
    private CompanyService companyService;
    
    public DataFetcher<Company> getCompanyByIdDataFetcher() {
        return dataFetchingEnvironment -> {
            
            logger.info("params:{}", dataFetchingEnvironment.getArguments());
            
            String selectionFieldNames = dataFetchingEnvironment.getSelectionSet().getFields().stream().map(model -> model.getName()).collect(Collectors.joining(","));
            logger.info("selectionFieldNames:{}", selectionFieldNames);
            
            Long companyId = Long.valueOf(dataFetchingEnvironment.getArgument("id"));
            return companyService.getCompanyById(companyId);
        };
    }
    
    public DataFetcher<List<Company>> getAllCompanyDataFetcher() {
      return dataFetchingEnvironment -> {
          
          logger.info("params:{}", dataFetchingEnvironment.getArguments());
          
          String selectionFieldNames = dataFetchingEnvironment.getSelectionSet().getFields().stream().map(model -> model.getName()).collect(Collectors.joining(","));
          logger.info("selectionFieldNames:{}", selectionFieldNames);
          
          return companyService.getCompanyList();
      };
    }
    
    public DataFetcher<Company> createCompanyDataFetcher() {
        return dataFetchingEnvironment -> {
            
            logger.info("params:{}", dataFetchingEnvironment.getArguments());
            
            String selectionFieldNames = dataFetchingEnvironment.getSelectionSet().getFields().stream().map(model -> model.getName()).collect(Collectors.joining(","));
            logger.info("selectionFieldNames:{}", selectionFieldNames);
            
            Map<String, Object> paramMap = dataFetchingEnvironment.getArguments();
            Map<String, Object> companyMap = (Map<String, Object>)paramMap.get("input");
            Company company = new Company();
            company.setId(Long.valueOf((String)companyMap.get("id")));
            company.setName((String)companyMap.get("name"));
            company.setAge((Integer)companyMap.get("age"));
            company.setAddress((String)companyMap.get("address"));
            company.setSalary((Double)companyMap.get("salary"));
            return companyService.createCompany(company);
        };
      }
    
    public DataFetcher<Company> updateCompanyDataFetcher() {
        return dataFetchingEnvironment -> {
            
            logger.info("params:{}", dataFetchingEnvironment.getArguments());
            
            String selectionFieldNames = dataFetchingEnvironment.getSelectionSet().getFields().stream().map(model -> model.getName()).collect(Collectors.joining(","));
            logger.info("selectionFieldNames:{}", selectionFieldNames);
            
            Map<String, Object> paramMap = dataFetchingEnvironment.getArguments();
            Map<String, Object> companyMap = (Map<String, Object>)paramMap.get("input");
            Company company = new Company();
            company.setId(Long.valueOf((String)companyMap.get("id")));
            company.setName((String)companyMap.get("name"));
            company.setAge((Integer)companyMap.get("age"));
            company.setAddress((String)companyMap.get("address"));
            company.setSalary((Double)companyMap.get("salary"));
            return companyService.updateCompany(company);
        };
      }
    
    public DataFetcher<Boolean> deleteCompanyDataFetcher() {
        return dataFetchingEnvironment -> {
            
            logger.info("params:{}", dataFetchingEnvironment.getArguments());
            
            String selectionFieldNames = dataFetchingEnvironment.getSelectionSet().getFields().stream().map(model -> model.getName()).collect(Collectors.joining(","));
            logger.info("selectionFieldNames:{}", selectionFieldNames);
            
            Long companyId = Long.valueOf(dataFetchingEnvironment.getArgument("id"));
            return companyService.deleteCompany(companyId);
        };
      }
    
}
